package com.settlement.system.service.impl;

import com.settlement.system.mapper.TblRpaChargeTaskMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.param.RpaChargeTaskUpdateApiRequestParams;
import com.settlement.system.model.entity.common.TblRpaChargeTask;
import com.settlement.system.common.util.common.DateUtil;
import com.settlement.system.service.ITblRpaChargeTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.settlement.system.mapper.BaseMapper;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.*;

/**
 * RPA3.0专项费用充值-任务表 服务实现类
 * @author xiaochuan
 * @since 2021-03-09
 */
@Slf4j
@Service
public class TblRpaChargeTaskServiceImpl extends BaseServiceImpl implements ITblRpaChargeTaskService {

    @Resource
    private TblRpaChargeTaskMapper tblRpaChargeTaskMapper;

    @Override
    public BaseMapper init() {
        return tblRpaChargeTaskMapper;
    }

    @Override
    public TblRpaChargeTask getFoot(Map<String, Object> params) throws ServiceException {
        try {
            return tblRpaChargeTaskMapper.getFoot(params);
        } catch (Exception ex) {
            log.error("获取合计异常", ex);
            throw new ServiceException("获取合计异常", ex);
        }
    }

    @Override
    @Transactional(rollbackFor = {ServiceException.class},noRollbackFor = {DuplicateKeyException.class})
    public void updateByParam(List<RpaChargeTaskUpdateApiRequestParams> models) throws ServiceException{
        try {
            for(RpaChargeTaskUpdateApiRequestParams model : models){
                tblRpaChargeTaskMapper.updateByParam(model);
            }
        } catch (Exception ex) {
            log.error("TblRpaChargeTaskServiceImpl.updateByParam error", ex);
            throw new ServiceException("TblRpaChargeTaskServiceImpl.updateByParam error", ex);
        }
    }

    @Override
    public List<Map<String,Object>> getAvailableBalance() throws ServiceException {
        try {
            Map<String, Object> params = new HashMap<>(16);
            String todayDate = DateUtil.getDate(new Date(), "yyyy-MM-dd");
            params.put("todayDate",todayDate);
            return tblRpaChargeTaskMapper.getAvailableBalance(params);
        } catch (Exception ex) {
            log.error("TblRpaChargeTaskServiceImpl.getAvailableBalance error", ex);
            throw new ServiceException("TblRpaChargeTaskServiceImpl.getAvailableBalance error", ex);
        }
    }

    @Override
    public TblRpaChargeTask getByDetailNumberId(String detailNumberId) throws ServiceException{
        try {
            return tblRpaChargeTaskMapper.getByDetailNumberId(detailNumberId);
        } catch (Exception ex) {
            log.error("获取合计异常", ex);
            throw new ServiceException("获取合计异常", ex);
        }
    }

    @Override
    public <ModelType> Integer saveNoDuplicateList(List<ModelType> list) throws ServiceException, DuplicateKeyException {
        List<String> detailNumberIdList = new ArrayList<>();
        for (int index = 0; index < list.size(); index++) {
            TblRpaChargeTask detail = (TblRpaChargeTask) list.get(index);
            String detailNumberId = detail.getDetailNumberId();
            if(detailNumberIdList.contains(detailNumberId)){
                throw new ServiceException("有两行明细序号ID一样");
            }else{
                detailNumberIdList.add(detailNumberId);
            }
        }
        return super.saveNoDuplicateList(list);
    }

}

